﻿function CompanyUserController($scope, service, $routeParams, $location, $http, $validator) {
    //$scope.emailCheck = false;
    var popupOptions = {
        appendTo: $("#popup-wrapper"),
        closeClass: 'b-close',
        speed: 300
    };
    $scope.user = {
        Email: "",
        PassWord: "",
        Name: "",
        RowStatus: 1
    }
    $scope.confirmPassword = $scope.user.PassWord;
    storeList();
    var type = "New"
    //get user list
    $http.get(service.locationUrl + "/companyuser/user/" + service.infoUser.Id, {})
        .success(function (data) {
            $scope.userList = data;
        })
        .error(function (err) {

            console.log("error", err);
        });
    //end
    //get data of Store list api/storelist/1
    function storeList()
    {
        $http.get(service.locationUrl + "/storelist/" + service.infoUser.Id, {})
        .success(function (data) {
            var allStores = data;
            $scope.allStores = allStores;
            var storesOfStaff = "";

            var isExist = function (store) {
                if (storesOfStaff) {
                    for (var i = 0; i < storesOfStaff.length; i++) {
                        if (storesOfStaff[i].CompanyStoreId == store.StoreId)
                            return true;
                    }
                }
                return false;
            }
            //This is the time to convert form staffs' ids to object that renderable to to store checkbox list
            _.each(allStores, function (store) {
                //Find in the storesOfStaff, if it exist, just simple set the Checked property to true
                store.Checked = isExist(store);
            });

            //Update the scope
            $scope._update($scope);
        })
        .error(function (err) {
            console.log("error", err);
        });
        //End get store

    }
    //Select user
    $scope.edit = function (id)
    {
        $scope.emailCheck = true;
        type = "Edit";
        $http.get(service.locationUrl + "/companyuserinformation/" + id, {})
        .success(function (data) {
            $scope.user = data;
            storeOfStaff(data.CompanyUserId);
            $scope.confirmPassword = $scope.user.PassWord;
        })
        .error(function (err) {

            console.log("error", err);
        });
    }
    //END

    function storeOfStaff(userAccessId) {
            //get data of Store list api/storelist/1
            $http.get(service.locationUrl + "/storelist/" + service.infoUser.Id, {})
                .success(function (data) {
                    var allStores = data;
                    $scope.allStores = allStores;

                    $http.get(service.locationUrl + "/userstore/companyuserId/" + userAccessId, {})
                      .success(function (data) {
                          var storesOfStaff = data;
                          var isExist = function (store) {
                              if (storesOfStaff) {
                                  for (var i = 0; i < storesOfStaff.length; i++) {
                                      if (storesOfStaff[i].CompanyStoreId == store.StoreId)
                                          return true;
                                  }
                              }
                              return false;
                          }
                          //This is the time to convert form staffs' ids to object that renderable to to store checkbox list
                          _.each(allStores, function (store) {
                              //Find in the storesOfStaff, if it exist, just simple set the Checked property to true
                              store.Checked = isExist(store);
                          });

                          //Update the scope
                          $scope._update($scope);
                      })
                      .error(function (err) { });

                })
                .error(function (err) {
                    console.log("error", err);
                });
            //End get store
        }

    //Save 
    $scope.save = function (userId, CompanyUserId)
    {
        $validator.validate($scope, "company")
           .success(function () {
               if (type == "Edit") {
                   // popup detele
                   $scope.title = "Do you want save ?";
                   $('.popup-delete-save').bPopup(popupOptions);
                   // End

                   // action when click yes update or create new
                   $scope.action = function () {
                       // Update conpany user 
                       var objCompanyuser =
                           {
                               Email: $scope.user.Email,
                               PassWord: $scope.user.PassWord,
                               CompanyId: service.infoUser.Id,
                               UserAccessId: userId,
                               Name: $scope.user.Name,
                               RowStatus: 1,
                           }
                       var storesOfuser = _.filter($scope.allStores, function (store) {
                           return store.Checked;
                       });

                       storesOfuser = _.map(storesOfuser, function (store) {
                           return store.StoreId;
                       });
                       objCompanyuser.StoreIds = storesOfuser;
                       console.log("anc", objCompanyuser);
                       //Put method update objCompanyuser
                       $http.put(
                           service.locationUrl + "/companyuser/" + CompanyUserId,
                           objCompanyuser)
                       .success(function () {
                           $('.popup-delete-save').bPopup().close();
                           //get user list
                           $http.get(service.locationUrl + "/companyuser/user/" + service.infoUser.Id, {})
                               .success(function (data) {
                                   $scope.userList = data;
                               })
                               .error(function (err) {

                                   console.log("error", err);
                               });
                           //end
                       })
                       .error(function (err) {
                           console.log(err);
                       })
                   //end
                   }
               }
               if (type == "New") {
                   $scope.title = "Do you want create new Staff ?";
                   $('.popup-delete-save').bPopup(popupOptions);
                   // End
                   $scope.action = function () {
                       var objCompanyuser =
                            {
                                Email: $scope.user.Email,
                                PassWord: $scope.user.PassWord,
                                CompanyId: service.infoUser.Id,
                                Name: $scope.user.Name,
                                RowStatus: 1,
                            }
                       var storesOfuser = _.filter($scope.allStores, function (store) {
                           return store.Checked;
                       });

                       storesOfuser = _.map(storesOfuser, function (store) {
                           return store.StoreId;
                       });
                       objCompanyuser.StoreIds = storesOfuser;
                       console.log("anc", objCompanyuser);
                       //Put method update objCompanyuser
                       $http.post(
                           service.locationUrl + "/companyuser/",
                           objCompanyuser)
                       .success(function () {
                           $('.popup-delete-save').bPopup().close();
                           //get user list
                           $http.get(service.locationUrl + "/companyuser/user/" + service.infoUser.Id, {})
                               .success(function (data) {
                                   $scope.userList = data;
                               })
                               .error(function (err) {

                                   console.log("error", err);
                               });
                           //end
                       })
                   }
               }
           }).error(function () {
               console.log("err");
           });
    }

    $scope.delete = function (id, CompanyUserId) {
        // popup detele
        $scope.title = "Do you want delete this staff ?";
        $('.popup-delete-save').bPopup(popupOptions);
        // End
        $scope.action = function () {
            // Update conpany user 
            var objCompanyuser =
                {
                    CompanyId: service.infoUser.Id,
                    UserAccessId: id,
                    Name: $scope.user.Name,
                    RowStatus: 0,
                }
            //Put method update objCompanyuser
            $http.put(
                "http://192.168.1.70:3000/api/companyuser/" + CompanyUserId,
                objCompanyuser)
            .success(function () {
                $scope.user = {
                    Email: "",
                    PassWord: "",
                    Name: "",
                    RowStatus: 1
                }
                $scope.confirmPassword = $scope.user.PassWord;
                $('.popup-delete-save').bPopup().close();
                //get user list
                $http.get("http://192.168.1.70:3000/api/companyuser/user/" + service.infoUser.Id, {})
                    .success(function (data) {
                        $scope.userList = data;
                    })
                    .error(function (err) {

                        console.log("error", err);
                    });
                //end
            })
            .error(function () { });
            // end put
        }
    }
    $scope.createNew = function (){
        $scope.emailCheck = false;
        type = "New";
        $scope.user = {
            Email: "",
            PassWord: "",
            Name: "",
            RowStatus: 1
        }
        $scope.confirmPassword = $scope.user.PassWord;
        storeList();
    }
}

